package com.getpebble.android.framework.receiver;

import android.content.BroadcastReceiver;
import android.content.ContentUris;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.provider.CalendarContract.CalendarAlerts;
import android.provider.CalendarContract.Events;
import android.util.LruCache;
import com.getpebble.android.R;
import com.getpebble.android.common.core.trace.Trace;
import com.getpebble.android.common.core.util.PblPreferences;
import com.getpebble.android.common.core.util.PblPreferences.PrefKey;
import com.getpebble.android.framework.notification.PblNotificationProcessor;
import com.getpebble.android.notifications.model.PblNotification;
import com.getpebble.android.notifications.model.PblNotification.LegacyNotification;
import com.getpebble.android.notifications.model.PblNotification.Source;
import java.text.DateFormat;
import java.util.Date;

public class CalendarReceiver extends BroadcastReceiver {
    private static final String[] CALENDAR_ALERT_PROJECTION = new String[]{"event_id", "alarmTime"};
    private static final String[] CALENDAR_EVENTS_PROJECTION = new String[]{"title", "description", "eventLocation", "dtstart", "dtend", "allDay", "_id"};
    private static final String TAG = CalendarReceiver.class.getSimpleName();
    private static final LruCache<Long, Boolean> processed = new LruCache(512);

    public void onReceive(Context context, Intent intent) {
        if (context == null) {
            Trace.warning(TAG, "Context is null!");
            return;
        }
        if (new PblPreferences(context).getBooleanData(PrefKey.CALENDAR_NOTIFICATIONS, true)) {
            long currentTime = System.currentTimeMillis();
            Cursor alarmCursor = context.getContentResolver().query(CalendarAlerts.CONTENT_URI, CALENDAR_ALERT_PROJECTION, "(state = 0 OR state = 1) AND alarmTime <= ?  AND end > ?", new String[]{Long.toString(currentTime), Long.toString(currentTime)}, "alarmTime");
            if (alarmCursor == null) {
                Trace.info(TAG, "Alarm cursor is null!");
                return;
            }
            while (alarmCursor.moveToNext()) {
                long eventKey = alarmCursor.getLong(0);
                Trace.debug(TAG, "key: " + String.valueOf(eventKey));
                if (processed.get(Long.valueOf(eventKey)) != null) {
                    Trace.verbose(TAG, "Already processed...");
                } else {
                    processed.put(Long.valueOf(eventKey), Boolean.valueOf(true));
                    Cursor eventCursor = context.getContentResolver().query(ContentUris.withAppendedId(Events.CONTENT_URI, alarmCursor.getLong(0)), CALENDAR_EVENTS_PROJECTION, null, null, null);
                    if (eventCursor == null) {
                        Trace.warning(TAG, "Failed to look-up calendar event");
                    } else {
                        String notificationText = null;
                        String title = null;
                        if (eventCursor.moveToNext()) {
                            title = eventCursor.getString(0);
                            String details = eventCursor.getString(1);
                            String location = eventCursor.getString(2);
                            Date begin = new Date(eventCursor.getLong(3));
                            Date end = new Date(eventCursor.getLong(4));
                            if (eventCursor.getInt(5) > 0) {
                            }
                            long eventId = eventCursor.getLong(6);
                            DateFormat localDateFormat = android.text.format.DateFormat.getDateFormat(context);
                            DateFormat localTimeFormat = android.text.format.DateFormat.getTimeFormat(context);
                            String strBegin = localDateFormat.format(begin) + " @ " + localTimeFormat.format(begin);
                            String strEnd = localDateFormat.format(end) + " @ " + localTimeFormat.format(end);
                            boolean isTitleNull = eventCursor.isNull(0);
                            boolean isDetailsNull = eventCursor.isNull(1);
                            boolean isLocationNull = eventCursor.isNull(2);
                            boolean isDateBeginNull = eventCursor.isNull(3);
                            boolean isDateEndNull = eventCursor.isNull(4);
                            if (isTitleNull) {
                                title = context.getString(R.string.calendar_event_title_reminder);
                            }
                            if (isDetailsNull) {
                                details = " ";
                            }
                            if (isLocationNull) {
                                location = " ";
                            }
                            if (isDateBeginNull) {
                                strBegin = " ";
                            }
                            if (isDateEndNull) {
                                strEnd = " ";
                            }
                            notificationText = "From: " + strBegin + "\nTo: " + strEnd + "\nLocation: " + location + "\nDetails: " + details;
                        }
                        if (title == null || notificationText == null) {
                            Trace.warning(TAG, "Content is null");
                            eventCursor.close();
                            alarmCursor.close();
                            return;
                        }
                        eventCursor.close();
                        LegacyNotification content = new LegacyNotification();
                        content.title = title;
                        content.body = notificationText;
                        PblNotificationProcessor.processNotification(PblNotification.from(content, Source.CALENDAR));
                    }
                }
            }
            alarmCursor.close();
        }
    }
}
